GBML manual / Backgrounds

Backgrounds

A background is a big picture built from 8x8 tiles that the hardware scrolls as one piece - not an instance, and only four can be shown at once.

Sprites and backgrounds

The GBA draws everything on screen in one of two ways. Sprites are the small pictures that belong to instances: a player, an enemy, a bullet. Backgrounds are the large pictures behind and around them: a floor, a sky, a wall of a cave. Drawing covers sprites. This page covers backgrounds.

A background is built from tiles, which are small 8x8-pixel squares of picture. A tileset is the sheet of tiles you draw, and the background is a grid that says which tile goes in which spot. Reusing tiles is what lets a background be far larger than the 240x160 screen without needing that much picture memory.

SpriteBackground
What it isone small image, up to 64x64 pixelsa whole grid of tiles, bigger than the screen
How many at once128 drawn per frame4 shown at once
Belongs toan instance, which has events and variablesnothing. It is a handle you hold in a variable
Moveseach one has its own positionthe whole layer has one position
Made froma sprite asseta tileset asset

A background has no events, no x and y variables, no collision and no Draw event. It is scenery. The engine also uses backgrounds itself. A room's floor is a background, and so is the dark overlay of a shrouded room. Those count toward the same limit of four as ones you make.

Pictures without code: the room's Backgrounds list

A room can put up background pictures with no code at all. In the Room Editor, the Tile Palette has a Backgrounds section. + Add background puts a picture in the room; for each one you choose the picture, where it sits (X and Y, counted from the room's top-left corner), whether it repeats past its own edge, and its scroll speed across and down (100 moves with the room, 50 half as far, 0 stays on the screen). The list is the draw order: the first row is in front, and you drag a row by its grip to move it. The room's own picture (its Tileset or Background Image) is one of the rows, so the pictures under it are behind it. The eyes are in Draw order, below the list.

A picture with a scroll speed other than 100 looks different wherever the camera is. The Room Editor shows it as it looks when the game starts the room, and repeats it across the whole room when its Repeat is on, so a room wider than the picture shows the picture more than once. In the game the picture moves more slowly than the camera, so you see only part of that row at a time.

The game draws exactly this list, the same as a background_create call and a draw_background call in an object would. A picture in the list uses one of the console's background layers like any other, so once the room has as many as the console can show, + Add background is grayed and says why. A picture behind the room's own picture needs a depth group set to Behind the background. Keep a picture in code when where it goes depends on the play (a sky that scrolls with the player's speed, a picture that changes with the story).

Making one

background_create(x, y, tileset) loads a background from a tileset and returns a handle to it. It starts hidden: nothing is on the screen yet. Keep that handle. The other functions in this category all need it, and there is no way to look one up afterwards.

// Create event of an object that is always in the room
backdrop = background_create(0, 0, ts_doc_dungeon);
if (backdrop != nothing) {
    background_priority(backdrop, 2);
}

Showing one

A background is shown by drawing it, like a sprite is. draw_background(bg, x, y) in a Draw event puts the picture's top-left corner at (x, y), counted from the room's top-left corner, for that frame. A background you do not draw in a frame is hidden in that frame, so to hide one, stop drawing it.

// Draw event of the same object
draw_background(backdrop, 0, 0);

What you draw later in the same event is in front of what you drew earlier, backgrounds and sprites together: a sprite drawn before a big solid background is hidden by it, a sprite drawn after it shows on top. In a Draw GUI event a background is in front of the room and the room's objects. draw_background has the details and how many switches fit.

Past the edge of the picture

The console repeats a background: move the view past the picture's edge and the same picture starts again, like tiles on a floor. That is what a background does until you say otherwise. To change it:

A background that stops repeating costs nothing if its picture is small, and otherwise takes one of the console's two rectangle windows (the Budgets panel's Windows row counts them). A clip always takes one. A zoomable background has its own switch, zoom_background_repeat.

Scrolling slower than the camera (parallax)

A picture can move less than the camera, so it looks far away. The setting is a number of percent: 100 moves with the room, 50 moves half as far, 0 stays fixed on the screen.

A speed adds no background layer, so it never uses up any of the layers the console has.

Check the result against nothing. With only four backgrounds available at once, and the room already using some, the call can fail. It then returns nothing instead of crashing. A room with three tile layers and a shroud has already used all four.

A background you create belongs to your project, not to the current room. It survives a room change, still holding its share of the budget, until you destroy it yourself.

That has one consequence worth knowing before it surprises you. A shrouded room draws its dark on a background layer of its own. If the backgrounds you left showing, plus the room's own picture, have already used every layer when the room loads, the room cannot get its dark, and it loads fully lit. The room never takes a layer away from a background you made, so the dark is the one that gives way. A development build stops there and tells you which room, and how many layers your backgrounds and the room's picture hold. Destroy a background before changing to that room, or untick Shrouded for it. A release build does not stop: it loads the room lit.

The room's own picture gets the same treatment. If the backgrounds you left showing have already used every layer when the room loads, a tile layer of the room has no layer to be drawn on, and the room loads without it: its tiles still block things, they are just not drawn. A development build stops there too, and tells you which room and which tile layer, and how many layers your backgrounds hold. Destroy a background before changing to that room, or give the room fewer tile layers. A release build does not stop: it loads the room without that picture.

Controlling one

FunctionDoes
draw_background(bg, x, y)shows it for this frame, in a Draw event, with its top-left corner at (x, y). A background you do not draw in a frame is hidden in that frame
background_priority(bg, priority)picks its layer, 0 to 3
background_destroy(bg)removes it
background_count()how many of your backgrounds exist

Two facts about the limits:

Moving one by destroying and re-creating it every frame is a mistake. Draw it somewhere else with draw_background.

Layers: what covers what

The hardware has four layers, numbered 0 (nearest the viewer) to 3 (furthest back). A new background starts on 3. The room claims some layers for itself. Layer 0 is the shroud, when the room has one. The room's own tile layers count down from 3, so a room's floor normally sits on 3.

Sprites and backgrounds meet through bg_priority, the layer number every instance has. A sprite draws in front of a background when its bg_priority is the same or lower, so a tie goes to the sprite. That is why an instance left at the default of 3 still appears in front of an ordinary room floor. Two backgrounds on the same layer do not conflict, but which one covers the other is not settled for you, so give them different layers. (The one exception is a zoomable background: when it and an ordinary background share a layer, the ordinary one always covers it.)

A background is shown in world space by a Draw event (it scrolls with the camera, like everything else in the room), and in a Draw GUI event it is fixed to the screen instead, so a menu backdrop or a dimming screen stays still while the camera moves.

A background that can zoom

Every background on this page sits where you put it and can only slide about. A room can also have one picture the hardware can make bigger, smaller and turn: a zoomable background, what a map screen wants. It costs that room two of its four layers, so a room opts in on its own, in Room Properties, and every other room keeps all four. A zoomable picture has its own rules (square, 8 bits, at most 256 different tiles), and the zoomable backgrounds page has them all.

The limits, in one place

LimitValue
Backgrounds shown at once, room and project together4 (2 in a room with a zoomable background)
Tiles in one background1,024
Background palette banks (16 colors each), a separate pool from sprite palettes16

Budgets explains all three. If a tile in the tileset is meant to animate, water for example, Tile animation covers how.

See also: Drawing and the draw orderThe cameraBudgetsbg_priorityTile animationRoomsZoomable backgroundsOne picture, several palettesbackground_repeatbackground_edgebackground_clipbackground_scroll_speed